package com.mornd.gulimall.product.controller;

import com.mornd.gulimall.common.core.result.AjaxResult;
import com.mornd.gulimall.product.entity.vo.spusave.SpecVo;
import com.mornd.gulimall.product.entity.vo.spusave.SpuSaveVo;
import com.mornd.gulimall.product.entity.vo.SpuVo;
import com.mornd.gulimall.product.service.ISpuInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * spu信息 前端控制器
 * </p>
 *
 * @author mornd
 * @since 2023-07-23
 */

@Validated
@RestController
@RequestMapping("/product/spuInfo")
public class SpuInfoController {
    @Resource
    private ISpuInfoService spuInfoService;

    @PutMapping
    // todo 防重复提交功能
    public AjaxResult save(@RequestBody @Validated SpuSaveVo spuSaveVo) {
        String id = spuInfoService.saveSpu(spuSaveVo);
        return AjaxResult.success((Object) id);
    }

    @GetMapping
    public AjaxResult list(SpuVo vo) {
        return AjaxResult.success(spuInfoService.list(vo));
    }

    /**
     * 修改 spu 的规格参数
     * @param spuId
     * @param specVoList
     * @return
     */
    @PostMapping("/updateSpec/{spuId}")
    public AjaxResult updateSpec(@PathVariable Long spuId,
                                 @RequestBody @NotEmpty List<@Valid SpecVo> specVoList) {
        spuInfoService.updateSpec(spuId, specVoList);
        return AjaxResult.success();
    }

    /**
     * spu 上架
     * @param spuId
     * @return
     */
    @PostMapping("/up/{spuId}")
    public AjaxResult spuUp(@PathVariable Long spuId) {
        spuInfoService.spuUp(spuId);
        return AjaxResult.success();
    }
}
